package com.itheima.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @Author Yjr
 * @Date 2025/6/21 10:28
 */
@Component
@Slf4j
public class SpringRabbitListener {

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage(String msg){
        log.info("接受到消息,消息内容如下:{}", msg);

    }

    @RabbitListener(queues = "work.queue")
    public void workListen1(String msg){
        log.info("workListen1接受到消息,消息内容如下:{}", msg);

        try {
            TimeUnit.MILLISECONDS.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = "work.queue")
    public void workListen2(String msg){
        log.error("workListen2接受到消息,消息内容如下:{}", msg);

        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = "fanout.queue1")
    public void fanoutListen1(String msg){
        log.info("fanoutListen1接受到消息,消息内容如下:{}", msg);

    }
    @RabbitListener(queues = "fanout.queue1")
    public void fanoutListen2(String msg){
        log.info("fanoutListen2接受到消息,消息内容如下:{}", msg);

    }

    @RabbitListener(queues = "fanout.queue2")
    public void fanoutListen3(String msg){
        log.error("fanoutListen3接受到消息,消息内容如下:{}", msg);

    }

    @RabbitListener(queues = "fanout.queue2")
    public void fanoutListen4(String msg){
        log.error("fanoutListen4接受到消息,消息内容如下:{}", msg);

    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者1接收到direct.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(String msg) {
        System.out.println("消费者2接收到direct.queue2的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String msg) {
        System.out.println("消费者1接收到topic.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String msg) {
        System.out.println("消费者2接收到topic.queue2的消息：【" + msg + "】");
    }

    @RabbitListener(queues = "object.queue")
    public void objectMsg(Map<String,Object> user) {
        System.out.println("消费者2接收到topic.queue2的消息：【" + user + "】");
    }

    /**
     * 创建惰性队列消费者
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(
            name = "lazy.queue",
            durable = "true",
            arguments = @Argument(name = "x-queue-mode",value = "lazy")
    ))
    public void lazyMsg(String msg){
        System.out.println("消费者接收到lazy.queue2的消息：【" + msg + "】");
    }

    /**
     * 创建优先队列消费者
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(
            name = "priority.queue",
            durable = "true",
            arguments = @Argument(name = "x-max-priority",value = "10",type = "java.lang.Integer")
    ))
    public void priorityMsg(String msg){

        System.out.println("消费者接收到priority.queue的消息：【" + msg + "】");
    }
}
